<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var pathSum = function (root, targetSum) {
        let count = 0
        function preOrder(root) {
          if (!root) return null
          backTracking(root, root.val)
          preOrder(root.left)
          preOrder(root.right)
        }
        preOrder(root)
        return count
        //每个节点作为根节点计算路径和
        function backTracking(root, sum) {
          if (!root) return
          if (sum == targetSum) {
            //注意，不是到叶子节点的路径和，是中途所有的短路径
            count++
          }
          if (root.left) {
            backTracking(root.left, sum + root.left.val)
          }
          if (root.right) {
            backTracking(root.right, sum + root.right.val)
          }
        }
      }
    </script>
  </body>
</html>
